using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;

namespace SharePointExtendedDetailsFeature.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("a52ff1b5-8b63-42ee-871d-a09596db8032")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // On feature activated create the list needed to support this feature.
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // Get the current SPSite from the feature properties
            using (SPSite site = (SPSite)properties.Feature.Parent)
            {
                // See if the list already exists
                SPList list = site.RootWeb.Lists.TryGetList(Constants.ListName);

                // If the list does not exist, create it
                if (list == null)
                {
                    // Add the list to the site and obtain a reference to it
                    Guid newListGuid = site.RootWeb.Lists.Add(Constants.ListName, Constants.ListDescription, SPListTemplateType.GenericList);
                    SPList newList = site.RootWeb.Lists[newListGuid];

                    // Mark the list as hidden
                    newList.Hidden = true;

                    // Create the fields we need

                    // Description
                    SPFieldMultiLineText fldDescription = (SPFieldMultiLineText)newList.Fields.CreateNewField(SPFieldType.Note.ToString(), Constants.FieldDescription);
                    fldDescription.NumberOfLines = 6;
                    fldDescription.RichText = true;
                    fldDescription.RichTextMode = SPRichTextMode.Compatible;

                    // Special Notes
                    SPFieldMultiLineText fldNotes = (SPFieldMultiLineText)newList.Fields.CreateNewField(SPFieldType.Note.ToString(), Constants.FieldNotes);
                    fldNotes.NumberOfLines = 6;
                    fldNotes.RichText = true;
                    fldNotes.RichTextMode = SPRichTextMode.Compatible;

                    // Guid of the list or site (for lookups)
                    SPFieldText fldGuid = (SPFieldText)newList.Fields.CreateNewField(SPFieldType.Text.ToString(), Constants.FieldGuid);

                    // Owner of the site or documentation
                    SPFieldUser fldOwner = (SPFieldUser)newList.Fields.CreateNewField(SPFieldType.User.ToString(), Constants.FieldOwner);

                    // Add the fields to the list
                    newList.Fields.Add(fldDescription);
                    newList.Fields.Add(fldNotes);
                    newList.Fields.Add(fldGuid);
                    newList.Fields.Add(fldOwner);

                    // Get the default view
                    SPView defView = newList.DefaultView;

                    // Add the fields to the default view
                    defView.ViewFields.Add(newList.Fields[Constants.FieldDescription]);
                    defView.ViewFields.Add(newList.Fields[Constants.FieldNotes]);
                    defView.ViewFields.Add(newList.Fields[Constants.FieldGuid]);
                    defView.ViewFields.Add(newList.Fields[Constants.FieldOwner]);

                    // Update the default view
                    defView.Update();

                    // Update the list with our changes
                    newList.Update();
                }
            }
        }

        // No actions performed on deactivation.  List will only be deleted in the feature is uninstalled.
        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // No actions performed on installation.
        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // On feature uninstalling delete the list that was created to support the feature.
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            // Get the current SPSite from the feature properties
            using (SPSite site = (SPSite)properties.Feature.Parent)
            {
                // See if the list already exists
                SPList list = site.RootWeb.Lists.TryGetList(Constants.ListName);

                // If the list exists, delete it
                if (list != null)
                {
                    site.RootWeb.Lists.Delete(list.ID);
                }
            }
        }

        // No actions performed on upgrading.
        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
